﻿using Abp.Domain.Entities;
using Abp.Domain.Entities.Auditing;
using shenghua.Common.CustomValueType;
using shenghua.Marketing.Coupons;
using shenghua.Order.Orders.Median;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace shenghua.Member.Coupons
{
    /// <summary>
    /// 会员优惠券
    /// </summary>
    public class MemberCoupon: Entity, ICreationAudited
    {
        public long MemberId { get; set; }
        public int CouponId { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        /// <summary>
        /// 优惠券画像
        /// </summary>
        public string Profile
        {
            get
            {
                return $"{Name}(满{Condition.MinAmount.Value}减{Amount.Value})";
            }
        }
        public CouponCondition Condition { get; set; }
        public TimeLimit TimeLimit { get; set; }
        public Money Amount { get; set; }
        public ValueArray<int> AppliedCommodityIds { get; set; }

        public CouponTargetType TargetType { get; set; }

        public MemberCouponStatus Status { get; set; }


        public bool IsContainCommodity(int commodityId)
        {
            if (TargetType == CouponTargetType.All)
            {
                return true;
            }
            else
            {
                return AppliedCommodityIds.Items.Contains(commodityId);
            }
        }

        public bool CanUse(List<CommodityCollectionItem> items)
        {
            if (!Enabled) { return false; }

            Money totalAmount = new Money();
            int commodityCount = 0;

            foreach (var item in items)
            {
                if (item.HasSalesPromotion) continue;
                if (TargetType != CouponTargetType.All && !IsContainCommodity(item.Commodity.CommodityId)) continue;
                commodityCount += item.Count;
                totalAmount += item.TotalAmount;
            }

            if (totalAmount.Value > Condition.MinAmount.Value &&
                commodityCount >= Condition.MinCount)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 优惠券是否有效
        /// </summary>
        public bool Enabled
        {
            get
            {
                DateTime now = DateTime.Now;
                return now >= TimeLimit.Begin && now <= TimeLimit.End && IsUnUsed;
            }
        }

        public bool IsUsed
        {
            get
            {
                return Status == MemberCouponStatus.Used || Status == MemberCouponStatus.Occupied;
            }
        }

        public bool IsUnUsed
        {
            get
            {
                return Status == MemberCouponStatus.Unused;
            }
        }

        public bool IsOverdue
        {
            get
            {
                return TimeLimit.IsOverdue;
            }
        }

        public long? CreatorUserId { get; set; }
        public DateTime CreationTime { get; set; }

        public MemberCoupon()
        {
            Condition = new CouponCondition();
            TimeLimit = new TimeLimit();
            Amount = new Money();
            AppliedCommodityIds = new ValueArray<int>();
        }
    }

    public enum MemberCouponStatus
    {
        /// <summary>
        /// 未使用
        /// </summary>
        Unused = 0,
        /// <summary>
        /// 被占用，在使用优惠券生成订单，并且未付款时，优惠券处于被占用状态，如果取消订单，则解除占用状态
        /// </summary>
        Occupied = 1,
        /// <summary>
        /// 已使用
        /// </summary>
        Used = 2
    }
}
